package com.ys.service.finance.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.entity.finance.TcwAccountCountForUser;
import com.ys.entity.finance.TcwUserPointFrozenRunning;
import com.ys.entity.finance.TcwUserPointRunning;
import com.ys.enume.finance.TcwUserPointRunningEnum;
import com.ys.mapper.finance.TcwAccountCountForUserMapper;
import com.ys.mapper.finance.TcwUserPointFrozenRunningMapper;
import com.ys.mapper.finance.TcwUserPointRunningMapper;
import com.ys.service.finance.TcwAccountCountForUserService;

@Service("tcwAccountCountForUserService")
public class TcwAccountCountForUserImpl implements TcwAccountCountForUserService {
	
	@Autowired
	private TcwAccountCountForUserMapper tcwAccountCountForUserMapper;
	@Autowired
	private TcwUserPointRunningMapper tcwUserPointRunningMapper;
	@Autowired
	private TcwUserPointFrozenRunningMapper tcwUserPointFrozenRunningMapper;
	
	/**
	 * 处理账户，并添加相应流水，只能在同一个方法理写，因为用到了同步锁
	 */
	@Override
	@Transactional
	public void change(ChangePointDto changePointDto, ChangePointFrozenDto changePointFrozenDto) throws Exception {
synchronized (this) {
		if (changePointDto != null && changePointFrozenDto == null) {
			/*
			 *  处理积分变化
			 *  1、积分余额和可用积分变动
			 *  2、积分流水添加
			 */
			String userId = changePointDto.getUserId();
			TcwAccountCountForUser account = new TcwAccountCountForUser().setUserId(userId).queryFirst();
			if (account == null) {
				throw new RuntimeException("账号不存在");
			}
			
			// 处理账上剩余资金和可用资金
			BigDecimal pointChange = changePointDto.getPointChange();
			account.setPoint(account.getPoint().add(pointChange))
				.setPointAble(account.getPointAble().add(pointChange));
			tcwAccountCountForUserMapper.update(account);
			
			// 添加积分流水
			{
				BigDecimal pointLeft = account.getPoint();
				String item = changePointDto.getItem();
				String itemSub = changePointDto.getItemSub();
				Integer type = changePointDto.getType();
				Long relativeId = changePointDto.getRelativeId();
				String userName = changePointDto.getUserName();
				String remark = changePointDto.getRemark();
				TcwUserPointRunning tcwUserPointRunning = TcwUserPointRunning.tcwUserPointRunning(userId, userName, item, itemSub, type, relativeId, pointChange, pointLeft, remark, 1);
				tcwUserPointRunningMapper.insert(tcwUserPointRunning);
			}
			return;
		} else if (changePointDto == null && changePointFrozenDto != null) {
			/*
			 *  处理积分冻结
			 *  1、冻结金额变动
			 *  2、冻结流水添加
			 */
			String userId = changePointFrozenDto.getUserId();
			TcwAccountCountForUser account = new TcwAccountCountForUser().setUserId(userId).queryFirst();
			if (account == null) {
				throw new RuntimeException("账号不存在");
			}
			
			// 处理账上冻结资金
			BigDecimal pointFrozenChange = changePointFrozenDto.getPointChangeFrozen();
			account.setPointFrozen(account.getPointFrozen().add(pointFrozenChange))
				.setPointAble(account.getPointAble().subtract(pointFrozenChange));
			tcwAccountCountForUserMapper.update(account);
			
			// 添加积分冻结流水
			{
				BigDecimal pointFrozenLeft = account.getPointFrozen();
				Integer type = changePointFrozenDto.getType();
				Long relativeId = changePointFrozenDto.getRelativeId();
				String userName = changePointFrozenDto.getUserName();
				String remark = changePointFrozenDto.getRemark();
				TcwUserPointFrozenRunning tcwUserPointFrozenRunning = TcwUserPointFrozenRunning.tcwUserPointFrozenRunning(
						userId, userName, type, relativeId, pointFrozenChange, pointFrozenLeft, remark, 1);
				tcwUserPointFrozenRunningMapper.insert(tcwUserPointFrozenRunning);
			}
		} else {
			/*
			 *  处理积分冻结和积分变动
			 *  a、处理积分冻结：1、冻结金额变动，积分余额和可用积分变动；2、冻结流水添加；3积分流水添加
			 */
			String userId = changePointFrozenDto.getUserId();
			TcwAccountCountForUser account = new TcwAccountCountForUser().setUserId(userId).queryFirst();
			if (account == null) {
				throw new RuntimeException("账号不存在");
			}
			
			// 处理账上冻结资金
			BigDecimal pointChange = changePointDto.getPointChange();
			BigDecimal pointFrozenChange = changePointFrozenDto.getPointChangeFrozen();
			account
				.setPointFrozen(account.getPointFrozen().add(pointFrozenChange))
				.setPoint(account.getPoint().add(pointChange))
				.setPointAble(account.getPointAble().add(pointChange));
			tcwAccountCountForUserMapper.update(account);
			
			// 添加积分冻结流水
			{
				BigDecimal pointFrozenLeft = account.getPointFrozen();
				Integer type = changePointFrozenDto.getType();
				Long relativeId = changePointFrozenDto.getRelativeId();
				String userName = changePointFrozenDto.getUserName();
				String remark = changePointFrozenDto.getRemark();
				TcwUserPointFrozenRunning tcwUserPointFrozenRunning = TcwUserPointFrozenRunning.tcwUserPointFrozenRunning(
						userId, userName, type, relativeId, pointFrozenChange, pointFrozenLeft, remark, 1);
				tcwUserPointFrozenRunningMapper.insert(tcwUserPointFrozenRunning);
			}
			
			// 添加积分流水
			{
				String item = changePointDto.getItem();
				String itemSub = changePointDto.getItemSub();
				BigDecimal pointLeft = account.getPoint();
				Integer type = changePointDto.getType();
				Long relativeId = changePointDto.getRelativeId();
				String userName = changePointDto.getUserName();
				String remark = changePointDto.getRemark();
				TcwUserPointRunning tcwUserPointRunning = TcwUserPointRunning.tcwUserPointRunning(
						userId, userName, item, itemSub, type, relativeId, pointChange, pointLeft, remark, 1);
				tcwUserPointRunningMapper.insert(tcwUserPointRunning);
			}
		
		}
}
	}
	
	/**
	 * 教务处流水处理 
	 */
	@Override
	@Transactional
	public void changePoint_JIAO_WU_CHU(String itemName, String itemNameSub, Integer type, Long relativeId, BigDecimal pointChange, String remark) throws Exception {
		/*
		 *  处理积分变化
		 *  1、积分余额和可用积分变动
		 *  2、积分流水添加
		 */
		String userId = "99999";
		String userName = "教务处";
		TcwAccountCountForUser account = new TcwAccountCountForUser().setUserId(userId).queryFirst();
		if (account == null) {
			throw new RuntimeException("账号不存在");
		}
		
		// 处理账上剩余资金和可用资金
		account.setPoint(account.getPoint().add(pointChange))
			.setPointAble(account.getPointAble().add(pointChange));
		tcwAccountCountForUserMapper.update(account);
		
		// 添加积分流水
		{
			BigDecimal pointLeft = account.getPoint();
			String item = itemName;
			String itemSub = itemNameSub;
			type = type + 1000;
			TcwUserPointRunning tcwUserPointRunning = TcwUserPointRunning.tcwUserPointRunning(userId, userName, item, itemSub, type, relativeId, pointChange, pointLeft, remark, 1);
			tcwUserPointRunningMapper.insert(tcwUserPointRunning);
		}
		return;
	}
}
